<?php

include '../../authorization_check.php';
include '../../checkVPCOnly.php';
require_once '../../commonutils/util/PageTemplate.php';
require_once '../../commonutils/db/DBConnectionManager.php';

function addJavaScripts($webPage){
	$javaScript = '

		function checkAddNewPaymentForm(){
			
			var workingHourPerWeek, paymentPerHour;
   			var name = window.document.addNewPaymentForm.selectName;
				
   			with(window.document.addNewPaymentForm){
   				workingHourPerWeek  = textWorkingHourPerWeek;
   				paymentPerHour  = textPaymentPerHour;
		   	}
   			
		   	if(name.value == -1){
		   		alert("First select a personnel");
		   		name.focus();
		   		return false;
		   	}
		   	
		   	if(workingHourPerWeek.value == ""){
		   		alert("Working Hour Per Week field can not be empty");
		   		workingHourPerWeek.focus();
		   		return false;
		   	}
		   	
		   	if(paymentPerHour.value == ""){
		   		alert("Payment Per Hour field can not be empty");
		   		paymentPerHour.focus();
		   		return false;
		   	}
		   	
    		return true;
		}
		
		';
	$webPage->addJavaScriptContent($javaScript);
	$webPage->addCommonJavaScripts();
}

function addAddNewPaymentForm($webPage){
	$webPage->addContent("<form name='addNewPaymentForm' method='post' action=''>");
	$webPage->addContent("<table align = 'center' border=1 name='addPaymentTable'>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th width=243 align=left>Name: </th>");
		$webPage->addContent("<td align=left width=343><select size=1 name=selectName>");
		$webPage->addContent("<option value=-1>-Select A Personnel-</option>");
			
			$result = DBConnectionManager::query("select personnelID, firstName, lastName from Personnel");
			
			while($row = mysql_fetch_assoc($result)){
				$personnelID = $row['personnelID']; 
				
				$numberOfRows = DBConnectionManager::num_of_rows("select * from PersonnelPayment where personnelID=$personnelID");
				
				//if a payment for this personnel has not been added before?
				if($numberOfRows == 0){
					$firstName = $row['firstName']; 
					$lastName = $row['lastName']; 
					$webPage->addContent("<option value=$personnelID>$firstName $lastName</option>");	
				}
			}
		$webPage->addContent("</select></td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th align=left>Working Hour Per Week:</th>");
		$webPage->addContent("<td align=left><input type=text name=textWorkingHourPerWeek onKeyPress='numericalOnly()'</td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th align=left>Number Of Weeks:</th>");
		$webPage->addContent("<td align=left width=343>");
		addNumberOfWeeksCombobox($webPage, 0, 14, '');
		$webPage->addContent("</td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th align=left>Payment Per Hour (YTL):</th>");
		$webPage->addContent("<td align=left><input type=text name=textPaymentPerHour onKeyPress='numericalOnly()'></td>");
	$webPage->addContent("<td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<td align=center colspan=2><input type=submit value='Add' onClick = 'return checkAddNewPaymentForm();' name=submitAdd><input type=reset value=Reset name=reset></td>");
	$webPage->addContent("</tr></table></form>");
}

function addNumberOfWeeksCombobox($webPage, $formIndex, $defaultValue, $finalized){
	
	$webPage->addContent("<select size=1 name=selectNumberOfWeeks[] $finalized>");
		
	for($i = 10; $i < 21; $i++){
		
		if($i != $defaultValue){
			$webPage->addContent("<option value=$i>$i</option>");
		}else{
			$webPage->addContent("<option selected value=$i>$i</option>");
		}
	}
		
	$webPage->addContent("</select>");
}

function queryPaymentList(){
	$result = DBConnectionManager::query("select * from Personnel p, PersonnelPayment pp 
				where 
				p.personnelID=pp.personnelID and 
				pp.programCode=$_SESSION[programCode] and 
				pp.semesterID=$_SESSION[semesterID]");
	
	return $result;
}

function listPayments($webPage){
	$webPage->addContent("<form name='paymentListForm' method='post'>");
	$webPage->addContent("<table align = 'center' border=1 name='listTable'>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th>Select</th>");
		$webPage->addContent("<th>ID</th>");
		$webPage->addContent("<th>Name</th>");
		$webPage->addContent("<th>Title</th>");
		$webPage->addContent("<th>Working Hour Per Week</th>");
		$webPage->addContent("<th>Number Of Weeks</th>");
		$webPage->addContent("<th>Total Working Hour</th>");
		$webPage->addContent("<th>Payment Per Hour (YTL)</th>");
		$webPage->addContent("<th>Total Payment (YTL)</th>");
	$webPage->addContent("</tr>");
	
		$result = queryPaymentList(); 
		$totalPayment = 0;
		
		while($row = mysql_fetch_assoc($result)){
			$personnelID = $row['personnelID']; 
			$firstName = $row['firstName']; 
			$lastName = $row['lastName']; 
			$title = $row['title'];
			$week = $row['week'];
			$weeklyHours = $row['weeklyHours'];
			$feePerWeek = $row['feePerWeek'];
			$finalized = ($row['finalized'] == 1) ? "disabled" : "";
			$totalWorkingHour = $weeklyHours * $week;
			$totalPaymentForAStaff = $totalWorkingHour * $feePerWeek; 
			$totalPayment += $totalPaymentForAStaff;
			$webPage->addContent("<tr>");
				$webPage->addContent("<td><input type=checkbox name='checkBoxSelect[]' value=$personnelID $finalized></td>");
				$webPage->addContent("<td><input type=hidden name='personnelID[]' value=$personnelID>$personnelID </td>");
				$webPage->addContent("<td>$firstName $lastName</td>");
				$webPage->addContent("<td>$title</td>");
				$webPage->addContent("<td align=left><input type=text name=textWeeklyHours[] value=$weeklyHours $finalized></td>");
				$webPage->addContent("<td>");
					addNumberOfWeeksCombobox($webPage, 1, $week, $finalized);
				$webPage->addContent("</td>");
				$webPage->addContent("<td>$totalWorkingHour</td>");
				$webPage->addContent("<td align=left><input type=text name=textPaymentPerHour[] value=$feePerWeek $finalized></td>");
				$webPage->addContent("<td>$totalPaymentForAStaff</td>");	
			$webPage->addContent("</tr>");
		}
	
	$webPage->addContent("<tr><td colspan = 7/></td><th align=left>TOTAL:</th><th align=left>$totalPayment</th>");
	$webPage->addContent("</tr>");
	$webPage->addContent("</table>");
	$webPage->addContent("<table align = 'center'><tr>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=submit value='Remove Selected(s)' name=submitRemove onClick= 'return confirmDelete()'>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=submit value='Finalize' name=submitFinalize>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=submit value='Update' name=submitUpdate>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=reset value='Reset' name=resetReset>");
	$webPage->addContent("</td>");
	$webPage->addContent("</tr></table></form>");
}

function addNewPayment(){
	
	$personnelID = $_POST['selectName'];
	$workingHourPerWeek = $_POST['textWorkingHourPerWeek'];
	$numberOfWeeks = $_POST['selectNumberOfWeeks'];
	$paymentPerHour = $_POST['textPaymentPerHour'];
	
	DBConnectionManager::query("insert into PersonnelPayment(week, weeklyHours, feePerWeek, 
	semesterID, programCode, personnelID) values($numberOfWeeks[0], $workingHourPerWeek, $paymentPerHour, $_SESSION[semesterID],
		$_SESSION[programCode], $personnelID)");
}

function removeSelectedPayments($webPage){
	
	$selectedCheckBoxes = $_POST['checkBoxSelect'];
	
	if(count($selectedCheckBoxes) == 0){
		$webPage->addJavaScriptContent("alert(\"You should first select a payament to remove\");");
		return;
	}
	
	foreach($selectedCheckBoxes as $aSelectedPaymentID){
		DBConnectionManager::query("delete from PersonnelPayment where personnelID=$aSelectedPaymentID and programCode=$_SESSION[programCode] and semesterID=$_SESSION[semesterID]");
	}
}

function updatePayments(){
	
	$personnelIDs = $_POST['personnelID'];
	
	for($i = 0; $i < count($personnelIDs); ++$i ){
		$personnelID = $personnelIDs[$i];
		$weeklyHours = $_POST['textWeeklyHours'][$i];
		$paymentPerHour = $_POST['textPaymentPerHour'][$i];
		$numberOfWeeks = $_POST['selectNumberOfWeeks'][$i];
		
		DBConnectionManager::query("update PersonnelPayment set week=$numberOfWeeks, weeklyHours=$weeklyHours, feePerWeek=$paymentPerHour where personnelID=$personnelID ");
	}
}

function finalizePayments(){
	
	$personnelIDs = $_POST['personnelID'];
	
	for($i = 0; $i < count($personnelIDs); ++$i ){
		$personnelID = $personnelIDs[$i];
		DBConnectionManager::query("update PersonnelPayment set finalized=1 where personnelID=$personnelID");
	}
}

include "Top.html";
include 'notifier.php';

$webPage = new Page();

addJavaScripts($webPage);

$webPage->addHeader('Administrative Staff and Research Assistant Payment Details');

if(isset($_POST['submitAdd'])){
	addNewPayment();
}else if(isset($_POST['submitRemove'])){
	removeSelectedPayments($webPage);
}else if(isset($_POST['submitUpdate'])){
	updatePayments();
}else if(isset($_POST['submitFinalize'])){
	finalizePayments();
}

addAddNewPaymentForm($webPage);
listPayments($webPage);

$webPage->addFooter(date('Y'), 'IIPIS');
echo $webPage->get();
include 'bottom.php';
?>